﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;
using System.Text;

namespace com.huigo.Admin.Web.order
{
    public partial class SalesOrderStatistics : BasePage
    {
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(fromDate.Text) ? Convert.ToDateTime(fromDate.Text) : DateTime.MinValue;
            }
        }
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(toDate.Text) ? Convert.ToDateTime(toDate.Text) : DateTime.MinValue;
            }
        }
        private string user
        {
            get
            {
                return string.IsNullOrEmpty(userName.SelectedValue) ? "0" : com.loto.Common.Utils.SqlEscape(userName.SelectedValue);
            }
        }
        private string product
        {
            get
            {
                return string.IsNullOrEmpty(productName.SelectedValue) ? string.Empty : com.loto.Common.Utils.SqlEscape(productName.SelectedValue);
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProductBind();
                UserBind();
            }
        }
        /// <summary>
        /// 产品列表
        /// </summary>
        private void ProductBind()
        {
            List<RequestFuncParams> list = new List<RequestFuncParams>();
            BaseService service = new BaseService();
            ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_PRODUCT_INFO_LIST, list);
            if (parser.RowsCount > 0)
            {
                List<DropList> cDropList = Tools.ParserToDropList(parser, "ProductId", "ProductName");
                if (cDropList != null)
                {
                    productName.DataSource = cDropList;
                    productName.DataValueField = "Id";
                    productName.DataTextField = "Value";
                    productName.DataBind();
                }
            }
        }
        /// <summary>
        /// 销售员列表
        /// </summary>
        private void UserBind()
        {
            List<RequestFuncParams> list = new List<RequestFuncParams>();
            BaseService service = new BaseService();
            ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_USER_DROPLIST, list);
            if (parser.RowsCount > 0)
            {
                List<DropList> cDropList = Tools.ParserToDropList(parser, "UserId", "UserRealName");
                if (cDropList != null)
                {
                    userName.DataSource = cDropList;
                    userName.DataValueField = "Id";
                    userName.DataTextField = "Value";
                    userName.DataBind();
                }
            }
        }
        private void BindData()
        {
            int type = 0;
            if (isProduct.Checked)
            {
                type = 1;
            }                       
            if (((queryStart != DateTime.MinValue && queryEnd != DateTime.MinValue) && (queryEnd > queryStart || queryEnd == queryStart)) || (queryEnd == DateTime.MinValue && queryStart == DateTime.MinValue))
            {
                if (((queryStart != DateTime.MinValue && queryEnd != DateTime.MinValue) && (queryEnd.AddMonths(-1) <= queryStart)) || (queryStart == DateTime.MinValue && queryEnd == DateTime.MinValue))
                {
                    List<RequestFuncParams> list = new List<RequestFuncParams>();
                    list.Add(new RequestFuncParams("start", queryStart.ToString()));
                    list.Add(new RequestFuncParams("end", queryEnd.ToString()));
                    list.Add(new RequestFuncParams("userId", user));
                    list.Add(new RequestFuncParams("productName", product));
                    list.Add(new RequestFuncParams("type", type.ToString()));
                    BaseService service = new BaseService();
                    ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_TOTAL_SALES, list);
                    if (parser.RowsCount > 0)
                    {
                        string productFid = string.Empty;
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("<table>");
                        sb.AppendLine("<thead>");
                        sb.AppendLine("<tr>");
                        sb.AppendLine("<th width=\"10%\">");
                        sb.AppendLine("销售员");
                        sb.AppendLine("</th>");
                        if (type == 1)
                        {                            
                            sb.AppendLine("<th width=\"20%\">");
                            sb.AppendLine("产品名称");
                            sb.AppendLine("</th>");
                        }
                        sb.AppendLine("<th width=\"10%\">");
                        sb.AppendLine("金额");
                        sb.AppendLine("</th>");
                        sb.AppendLine("<th width=\"6%\">");
                        sb.AppendLine("操作");
                        sb.AppendLine("</th>");
                        sb.AppendLine("</tr>");
                        sb.AppendLine("</thead>");
                        sb.AppendLine("<tbody>");
                        for (int i = 0; i < parser.RowsCount; i++)
                        {
                            if (type == 1)
                            {
                                productFid = parser["OrderProductFid", i];
                            }
                            sb.AppendLine("<tr>");
                            sb.AppendLine("<td>");
                            sb.AppendLine(parser["UserName", i]);
                            sb.AppendLine("</td>");
                            if (type == 1)
                            {
                                sb.AppendLine("<td>");
                                sb.AppendLine(parser["OrderProductName", i]);
                                sb.AppendLine("</td>");
                            }
                            sb.AppendLine("<td>");
                            sb.AppendLine(string.Format("{0:C2}", Convert.ToDecimal(parser["Price", i])));
                            sb.AppendLine("</td>");
                            sb.AppendLine("<td>");
                            sb.AppendLine("<a href=\"javascript:GetSalesDetail('" + parser["UserId", i] + "','" + Convert.ToDateTime(queryStart).ToString("yyyy-MM-dd") + "','" + Convert.ToDateTime(queryEnd).ToString("yyyy-MM-dd") + "','" + productFid + "')\">查看明细</a>");
                            sb.AppendLine("</td>");
                            sb.AppendLine("</tr>");
                        }
                        sb.AppendLine("</tbody>");
                        sb.AppendLine("</table>");
                        litSettlement.Text = sb.ToString();
                    }
                    else
                    {
                        litSettlement.Text = string.Empty;
                    }
                }
                else
                {
                    MessageBox.Show(this.Page, "时间范围不能超过一个月");
                    litSettlement.Text = string.Empty;
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.Page, "结束时间要大于开始时间,，重新选择时间");
                litSettlement.Text = string.Empty;
                return;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}
